/*! 全局词典 */
(function (global) {
  // 项目代码，name不要包含“基地”结尾
  var siteCode = {
    ZSSJ: { name: '中山三角', bucket: 'ly_iot' },
    ZHDM: { name: '珠海斗门', bucket: 'ly_iot' },
    FSZN: { name: '佛山紫南', bucket: 'ly_iot' },
    HBEZ: { name: '湖北鄂州', bucket: 'ly_iot_hb' },
    HBLZH: { name: '湖北梁子湖', bucket: 'ly_iot_hblzh' },
    SZLY: { name: '深圳良友', bucket: 'ly_iot_sz' },
    AHAQ: { name: '安徽安庆', bucket: 'ly_iot_aqylf' },
    AHJX: { name: '安徽绩溪', bucket: 'ly_iot_AHJX' },
    SXGX: { name: '山西古县', bucket: 'ly_iot_SXGX' },
    ShXWN: { name: '陕西渭南', bucket: 'ly_iot_ShXWN' },
  };

  // 设备缩写 -> 获取设备信息
  var deviceByAbbr = {
    DO: { name: '溶氧传感器', type: 'do' },
    DO3: { name: '溶解臭氧传感器', type: 'do3' },
    PLC: { name: 'PLC设备', type: 'plc' },
    AO3: { name: '臭氧温湿度传感器', type: 'air_o3' },
    WL: { name: '液位传感器', type: 'water_level' },
    SW: { name: '继电器传感器', type: 'switch' },
    O3WP: { name: '溶解臭氧混合压力传感器', type: 'o3_water_pressure' },
    NO2: { name: '亚硝酸盐传感器', type: 'no2' },
    NH3: { name: '氨氮传感器', type: 'nh3' },
    NHN: { name: 'TAN传感器', type: 'nhn' },
    O3P: { name: '臭氧压力传感器', type: 'ozone_pressure' },
    IVR: { name: '变频器', type: 'inverter' },
  };

  // 设备安装位置缩写 -> 获取设备安装位置信息
  var devicePointByAbbr = {
    ba: { name: '平衡池', type: 'bal' },
    be: { name: '前端', type: 'bef' },
    af: { name: '末端', type: 'aft' },
    fp: { name: '鱼池', type: 'fp' },
    mx: { name: '混合', type: 'mix' },
    bk: { name: '回水', type: 'bkw' },
    pp: { name: '泵池', type: 'pump' },
    tk: { name: '蓄水池', type: 'tank' },
    no: { name: '号', type: 'no' },
  };

  // 字段缩写 → 获取字段信息
  var fieldByAbbr = {
    t: { field: 'temperature', name: '水温', unit: '℃' },
    do: { field: 'oxygen', name: '溶解氧', unit: 'mg/L' },
    ph: { field: 'PH', name: 'PH', unit: 'pH' },
    wl: { field: 'water_level', name: '水位', unit: 'cm' },
    do3: { field: 'ozone', name: '溶解臭氧', unit: 'mg/L' },
    do3t: { field: 'ozone_temperature', name: '溶解臭氧传感器温度', unit: '℃' },
    ao3: { field: 'air_o3', name: '空气臭氧', unit: 'mg/L' },
    at: { field: 'air_temperature', name: '空气温度', unit: '℃' },
    ah: { field: 'air_humidity', name: '空气湿度', unit: '%' },
    no2: { field: 'no2', name: '亚硝酸盐', unit: 'mg/L' },
    nh3: { field: 'nh3', name: '氨氮', unit: 'mg/L' },
    nhn: { field: 'nhn', name: 'TAN', unit: 'mg/L' },
    do3ws: { field: 'o3_water_switch', name: '臭氧混合继电器', unit: '' },
    do3wp: { field: 'o3_water_pressure', name: '臭氧混合压力', unit: 'Pa' },
    hh: { field: 'hour', name: '小时', unit: 'h' },
    mm: { field: 'minute', name: '分钟', unit: 'm' },
    ss: { field: 'second', name: '秒', unit: 's' },
    YYYY: { field: 'year', name: '年', unit: 'y' },
    MM: { field: 'month', name: '月', unit: 'm' },
    DD: { field: 'day', name: '日', unit: 'd' },
    v: { field: 'power_v', name: '电压', unit: 'V' },
    a: { field: 'power_a', name: '电流', unit: 'A' },
    w: { field: 'power_w', name: '功率', unit: 'W' },
    stu: { field: 'status', name: '状态', unit: '' },
    ol: { field: 'overload', name: '负载报警', unit: '' },
    rhz: { field: 'runing_hz', name: '运行频率', unit: 'hz' },
    shz: { field: 'setup_hz', name: '设置频率', unit: 'hz' },
    bv: { field: 'bus_volt', name: '母线电压', unit: 'V' },
    ov: { field: 'out_volt', name: '输出电压', unit: 'V' },
    oa: { field: 'out_ampere', name: '输出电流', unit: 'A' },
    rrpm: { field: 'runing_rpm', name: '运行转速', unit: 'rpm' },
    opr: { field: 'out_power_ratio', name: '输出功率比', unit: '%' },
    otr: { field: 'out_torque_ratio', name: '输出转矩比', unit: '%' },
  };

  var deviceInfoTable = {};

  function getDeviceInfo(abbrField) {
    if (deviceInfoTable[abbrField]) {
      // 返回兼容es5的深拷贝
      return JSON.parse(JSON.stringify(deviceInfoTable[abbrField]));
    }
    var abbrs = abbrField.split(':');
    var deviceArr = abbrs[0].split('_');
    var deviceAbbr = deviceArr[0];

    if (!deviceByAbbr[deviceAbbr]) {
      return;
    }

    var devicePointAbbr = deviceArr[1];
    var fieldAbbr = abbrs[1];
    var no = deviceArr.length > 2 ? deviceArr[2] : '';
    var deviceInfo = {
      tags: {
        device: deviceByAbbr[deviceAbbr].name,
        deviceAbbr: deviceAbbr,
        point: devicePointByAbbr[devicePointAbbr]
          ? devicePointByAbbr[devicePointAbbr].name
          : '',
        deviceId: deviceAbbr + '_' + (devicePointAbbr || ''),
        deviceType: deviceByAbbr[deviceAbbr].type,
        no: no,
        abbr: fieldAbbr,
      },
      field: fieldByAbbr[fieldAbbr] ? fieldByAbbr[fieldAbbr].field : fieldAbbr,
    };
    deviceInfoTable[abbrField] = deviceInfo;
    // 返回兼容es5的深拷贝
    return JSON.parse(JSON.stringify(deviceInfo));
  }

  // 预设
  var presetDevices = [
    'DO_ba:t',
    'DO_ba:do',
    'DO_ba:ph',
    'DO_be:t',
    'DO_be:do',
    'DO_be:ph',
    'DO_af:t',
    'DO_af:do',
    'DO_af:ph',
    'DO_fp_1:t',
    'DO_fp_1:do',
    'DO_fp_1:ph',
    'DO_fp_2:t',
    'DO_fp_2:do',
    'DO_fp_2:ph',
    'DO_fp_3:t',
    'DO_fp_3:do',
    'DO_fp_3:ph',
    'DO_fp_4:t',
    'DO_fp_4:do',
    'DO_fp_4:ph',
    'DO_fp_5:t',
    'DO_fp_5:do',
    'DO_fp_5:ph',
    'DO_fp_6:t',
    'DO_fp_6:do',
    'DO_fp_6:ph',
    'WL_ba:wl',
    'WL_pp:wl',
    'WL_pp_1:wl',
    'WL_pp_2:wl',
    'WL_tk:wl',
    'WL_tk_1:wl',
    'WL_tk_2:wl',
    'DO3_mx:do3',
    'DO3_mx:do3t',
    'DO3_mx_1:do3',
    'DO3_mx_1:do3t',
    'DO3_bk:do3',
    'DO3_bk:do3t',
    'DO3_bk_1:do3',
    'DO3_bk_1:do3t',
    'AO3:at',
    'AO3:ah',
    'AO3:ao3',
    'AO3_no_1:at',
    'AO3_no_1:ah',
    'AO3_no_1:ao3',
    'IVR_no_1:stu',
    'IVR_no_1:rhz',
    'IVR_no_1:ov',
    'IVR_no_1:oa',
  ];

  presetDevices.forEach(function (key) {
    getDeviceInfo(key);
  });

  // console.log(deviceInfoTable);

  // UMD 暴露
  global.dictMap = {
    siteCode: siteCode,
    deviceByAbbr: deviceByAbbr,
    fieldByAbbr: fieldByAbbr,
    devicePointByAbbr: devicePointByAbbr,
  };
  global.getDeviceInfo = getDeviceInfo;
})(global);

export default {};
